package com.cjk.console.ctrl;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjk.console.SessionApi;
import com.cjk.console.request.DeviceRequest;
import com.cjk.console.service.DeviceOperateService;
import com.cjk.console.service.DeviceService;
import com.cjk.foundation.commons.response.PageResponse;
import com.cjk.foundation.commons.response.Response;

@Controller
@RequestMapping("/device")
public class DeviceController {
	
	@Autowired
	private DeviceService deviceService;
	
	@Autowired
	private DeviceOperateService deviceOperateService;
	
	@ResponseBody
	@RequestMapping(value = "/getDeviceList.do", method = RequestMethod.POST)
	public PageResponse getDeviceList(@RequestBody DeviceRequest request, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		return deviceService.getDeviceList(request, userId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getDevice.do", method = RequestMethod.POST)
	public Response getDevice(String sn, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		return deviceService.getDevice(sn, userId);
	}

	@ResponseBody
	@RequestMapping(value = "/addDevice.do", method = RequestMethod.POST)
	public Response addDevice(@RequestBody DeviceRequest request, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		deviceService.addDevice(request, userId);
		return Response.success();
	}
	
	/**
	 * 批量删除设备
	 * @param request
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delDevice.do", method = RequestMethod.POST)
	public Response delDevice(@RequestBody DeviceRequest request, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		return deviceService.delDevice(request, userId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/reboot.do", method = RequestMethod.POST)
	public Response reboot(@RequestBody DeviceRequest request, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		return deviceOperateService.reboot(request, userId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/configDevice.do", method = RequestMethod.POST)
	public Response configDevice(@RequestBody DeviceRequest request, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		return deviceOperateService.configDevice(request, userId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getMgt.do", method = RequestMethod.POST)
	public Response getMgt(@RequestBody DeviceRequest request) {
		return deviceService.getMgt(request);
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateAliasName.do", method = RequestMethod.POST)
	public Response updateAliasName(String sn, String aliasName) {
		return deviceService.updateAliasName(sn, aliasName);
	}
	
}
